import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios'
Vue.use(Vuex);

export const store=new Vuex.Store({
    state:{
        nomore:false,
        newsList:[],    
        newsClssify:[],
        production:[],
        productionClassify:[],
        carts:[],
        ids:[],
        cartsNum:0,
        totalMoney:0,

    },
    getters:{
        newsList: state=>{
            return state.newsList
        },
        nomore: state=>{
            return state.nomore
        },
        newsClssify:state=>{
            return state.newsClssify
        },
        production:state=>{
            return state.production
        },
        productionClassify:state=>{
            return state.productionClassify
        },
        carts:state=>{
            return state.carts
        },
        cartsNum:state=>{
            return state.cartsNum
        },
        totalMoney:state=>{
            return state.totalMoney
        }
    },
    mutations:{
            getNewsList:(state,payload)=>{
               axios.post(`http://localhost:3000/news/list`,payload)
                .then(res=>{
                    var pageTotal=res.data.pages;
                    if(payload){
                        if(payload.page>1){
                            state.newsList=state.newsList.concat(res.data.rows);
                            if(payload.page>pageTotal){
                                state.nomore=true
                            }
                        }else{
                            state.newsList=res.data.rows
                        }
                    }else{
                        state.newsList=res.data.rows
                    }
                })
            },
            getNewsClassify:(state,payload)=>{
                axios.get("http://localhost:3000/classify/list/1",payload)
                .then((res)=>{
                    state.newsClssify=res.data;
                })
            },
            getProduction:(state,payload)=>{
                axios.post(`http://localhost:3000/production/list`,payload)
                .then(res=>{
                    state.production=res.data.rows;
                })
            },
            getProductionClassify:(state,payload)=>{
                axios.get("http://localhost:3000/classify/list/3",payload)
                .then(res=>{
                    state.productionClassify=res.data;
                })
            },
            getCarts:(state,payload)=>{

                if(state.ids.indexOf(payload.id)==-1){
                    state.ids.push(payload.id);
                    axios.get(`http://localhost:3000/production/data/`+payload.id)
                    .then(res=>{                   
                        res.data.num=1;
                        state.carts.unshift(res.data)  
                        state.cartsNum=0;
                        state.totalMoney=0;
                        for(var j=0;j<state.carts.length;j++){
                            state.cartsNum+=Number(state.carts[j].num)
                            state.totalMoney+=Number(state.carts[j].num)*Number(state.carts[j].price)
                            console.log(state.totalMoney) 
                        }
                    })
                 }else{
                     for (var i = 0; i < state.carts.length ;i++) {
                        if (state.carts[i]._id == payload.id) {
                            break;
                        }
                    }
                     state.carts[i].num+=1    
                     state.cartsNum=0;
                     state.totalMoney=0;
                     for(var j=0;j<state.carts.length;j++){
                         state.cartsNum+=Number(state.carts[j].num)
                         state.totalMoney+=Number(state.carts[j].num)*Number(state.carts[j].price)
                         console.log(state.totalMoney) 
                     }                
                 }
                 console.log(state.carts) 
                 
            }
    },
    actions:{
        getNewsList: ({commit},payload)=>{
            commit('getNewsList',payload)
        },
        getNewsClassify: ({commit},payload)=>{
            commit('getNewsClassify',payload)
        },
        getProduction:({commit},payload)=>{
            commit('getProduction',payload)
        },
        getProductionClassify:({commit},payload)=>{
            commit('getProductionClassify',payload)
        },
        getCarts:({commit},payload)=>{
            commit('getCarts',payload)
        }
    }
})